package com.businessportal.web.common;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Vadim on 7/4/14.
 */
public class DateTimeHelper {

    private static String[] patterns = {//"EEE, dd MMM yyyy hh:mm:ss UTC",
            "dd-MM-yyyy HH:mm",
            "yyyy.MM.dd G 'at' HH:mm:ss z",
            "EEE, MMM d, ''yy",
            "yyyyy.MMMMM.dd GGG hh:mm aaa",
            "EEE, d MMM yyyy HH:mm:ss Z",
            "yyMMddHHmmssZ",
            "d MMM yyyy HH:mm:ss z",
            "yyyy-MM-dd'T'HH:mm:ss",
            "yyyy-MM-dd'T'HH:mm:ss'Z'",
            "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
            "yyyy-MM-dd'T'HH:mm:ssZ",
            "yyyy-MM-dd'T'HH:mm:ss.SSSZ",
            "yyyy-MM-dd'T'HH:mm:ssz",
            "yyyy-MM-dd'T'HH:mm:ss.SSSz",
            "EEE, d MMM yy HH:mm:ssz",
            "EEE, d MMM yy HH:mm:ss",
            "EEE, d MMM yy HH:mm z",
            "EEE, d MMM yy HH:mm Z",
            "EEE, d MMM yyyy HH:mm:ss z",
            "EEE, d MMM yyyy HH:mm:ss Z",
            "EEE, d MMM yyyy HH:mm:ss ZZZZ",
            "EEE, d MMM yyyy HH:mm z",
            "EEE, d MMM yyyy HH:mm Z",
            "d MMM yy HH:mm z",
            "d MMM yy HH:mm:ss z",
            "d MMM yyyy HH:mm z",
            "d MMM yyyy HH:mm:ss z"};


    public static Date parseDateFromString(String strDate) throws Exception {
        if (strDate.contains("+")) {
            strDate = strDate.substring(0, strDate.lastIndexOf("+") - 1);
        }

        for (String item  : patterns) {
            SimpleDateFormat sdf = new SimpleDateFormat(item);
            try {
                return sdf.parse(strDate);
            } catch (Exception e) {
            }
        }

        throw new Exception("Couldn't parse date '" + strDate + "'");
    }

    public static String dateFormatForCalendar(Date date){
        SimpleDateFormat dt1 = new SimpleDateFormat("dd-MM-yyyy HH:mm");
        return dt1.format(date);
    }
}
